package com.kun.index.controller;


import com.kun.common.ResultDto;
import com.kun.index.entity.Blog;
import com.kun.index.entity.Comment;
import com.kun.index.service.impl.CommentServiceImpl;
import com.kun.index.service.impl.UserServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 评论表 前端控制器
 * </p>
 *
 * @author kunkun
 * @since 2024-03-12
 */
@RestController
@RequestMapping("/index/comment")
public class CommentController {

    @Autowired
    private CommentServiceImpl commentService;

    @Autowired
    private UserServiceImpl userService;

    @GetMapping("/commitComment")
    @ApiOperation("提交评论")
    public ResultDto commitComment(@RequestParam("blogId") Integer blogId,
                                   @RequestParam("userId") Integer userId,
                                   @RequestParam("content") String content){
        Comment comment = new Comment();
        comment.setBlogId(blogId);
        comment.setUserId(userId);
        comment.setContent(content);
        Integer num = commentService.insertComment(comment);
        return ResultDto.success(num);

    }

    @PostMapping("/commitFirstReply")
    @ApiOperation("提交第一次回复")
    public ResultDto commitFirstReply(@RequestBody Comment comment){
        Integer num = commentService.insertComment(comment);
        return ResultDto.success(num);

    }

    @GetMapping("/queryComments")
    @ApiOperation("通过博客id查询评论")
    public ResultDto queryComments(@RequestParam("blogId") Integer blogId){

        List<Comment> comments = commentService.queryCommentsByBlogId(blogId);
        for (Comment comment : comments) {
            comment.setUserNickname(userService.queryUserById(comment.getUserId()).getNickname());
            comment.setUserAvatarUrl(userService.queryUserById(comment.getUserId()).getAvatarUrl());
            if (comment.getRId()==null){
                ArrayList<Comment> list = new ArrayList<>();
                comment.setChildComments(list);
            }
            if (comment.getPId()!=null){
                Integer userId = commentService.queryCommentById(comment.getPId()).getUserId();
                comment.setPNickname(userService.queryUserById(userId).getNickname());
            }

        }
        //筛选所有非根评论
        ArrayList<Comment> notRComments = new ArrayList<>();
        notRComments=(ArrayList<Comment>)comments.stream().filter(res->(res.getRId()!=null)).collect(Collectors.toList());
        //对于每个根评论设置其子评论
        for (Comment notRComment : notRComments) {
            for (Comment comment : comments) {
                if (comment.getRId()==null && notRComment.getRId()==comment.getId()){
                    comment.getChildComments().add(notRComment);
                    break;
                }
            }
        }
        comments=(ArrayList<Comment>)comments.stream().filter(res->(res.getRId()==null)).collect(Collectors.toList());

        return ResultDto.success(comments);

    }



}

